define(['services/services'], function (services) {
    'use strict';
    services.factory('authenFactory', ['apiUrl','$rootScope','$q',function (apiUrl,$rootScope,$q) {
        var interceptor = {
            'request': function (request) {
                if(request.url.indexOf(".css")==-1&&request.url.indexOf(".html")==-1){
                    request.headers["Authorization"]="Bearer "+$rootScope.token;
                }
                // 成功的请求方法
                return request; // 或者 $q.when(config);
            },
            responseError: function (response) {
                var status = response.status;
                // 判断错误码，如果是未登录
                if(status == "401"){
                    // 全局事件，方便其他view获取该事件，并给以相应的提示或处理
                    $rootScope.$emit("userIntercepted","notLogin",response);
                    return $q.reject(response);
                }
                return $q.reject(response);
            }
        };
        return interceptor;
    }]);
});